package com.example.hospitalbakcends.controller;

import com.example.hospitalbakcends.domain.Medicines;
import com.example.hospitalbakcends.service.MedicinesService;
import com.example.hospitalbakcends.utils.RoleCheck;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/Medicine")
public class MedicineController {
    @Resource
    private MedicinesService medicinesService;

    /**
     * 药品登记
     * @param medicine
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/login")
    public boolean medicinesLogin(@RequestBody Medicines medicine, HttpServletRequest httpServletRequest){
        if(medicine==null){
            return false;
        }
        return medicinesService.save(medicine);
    }

    /**
     * 药品更新
     * @param medicine
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/update")
    public int medicineUpdate(@RequestBody Medicines medicine, HttpServletRequest httpServletRequest){
        if(medicine==null){
            return -1;
        }
        medicinesService.updateById(medicine);
        return 1;
    }

    /**
     * 药品删除
     * @param id
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/delete")
    public boolean medicineDelete(@RequestBody int id,HttpServletRequest httpServletRequest){
        if (!RoleCheck.isAdmin(httpServletRequest)) {
            return false;
        }
        return medicinesService.removeById(id);
    }

    /**
     * 查询所有药品信息
     * @return
     */
    @GetMapping ("/AllMedicinesInfo")
    public List<Medicines> getAllDmedicineInfo(){
        return medicinesService.list();
    }

    /**
     * 根据名称查询药品信息
     * @param medName
     * @return
     */
    @PostMapping("/MedicineInfo")
    public Medicines getMedicineById(@RequestBody String medName){
        if(medName==null){
            return null;
        }
        return medicinesService.getByName(medName);
    }


    /**
     * 根据ID查询药品信息
     * @param id
     * @return
     */
//    @PostMapping("/MedicineInfo")
//    public Medicines getMedicineById(@RequestBody long id){
//        if(id<0){
//            return null;
//        }
//        return medicinesService.getById(id);
//    }
//    方法error
}
